package com.qkjia.day01;

public class Test02_synchronized {
    public static void main(String[] args) {
        init();
    }
    public static void init() {
        Output output = new Output();
        new Thread(()->{
            while (true){
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                output.print1("atguigu");
            }
        }).start();

        new Thread(()->{
            while (true){
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Output.print2("zhangsan");
            }
        }).start();
    }
}
class Output{
    public void print1(String name){
        int length = name.length();
        synchronized (Output.class){
            for (int i = 0; i <length ; i++) {
                System.out.print(name.charAt(i));
            }
            System.out.println();
        }
    }
    public static synchronized void print2(String name){
        int length = name.length();
        for (int i = 0; i <length ; i++) {
            System.out.print(name.charAt(i));
        }
        System.out.println();
    }
}
